<template>
    <div class="main-content">
        <echartsLayout :formState='formState' :dataListState='dataListState' :eCharts1State='eCharts1State' :eCharts2State='eCharts2State' 
        :eCharts3State='eCharts3State' :chartsListState='chartsListState' :tableState='tableState'>
            <div slot="form">
                <el-form :model="query" ref="ruleForm" label-width="75px" label-position="right" :rules="rules">
                    <el-row :gutter="41">
                        <el-col :span="7">
                            <el-form-item label="e学云号" prop="eID">
                                <el-input v-model="query.eID" placeholder="请输入e学账号" style="width:100%" :maxlength="9"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="报表日期" prop="reportDate">  
                                 <el-row>
                                    <el-col :span="11">
                                        <el-date-picker v-model="query.startDate" :clearable="false" :editable="false" type="date" placeholder="选择开始日期" :picker-options="pickerOptions1" @change="startDateChange" style="width:100%"></el-date-picker>
                                    </el-col>
                                    <el-col :span="2" align="center">-</el-col>
                                    <el-col :span="11">
                                        <el-date-picker v-model="query.endDate" :clearable="false" :editable="false" type="date" placeholder="选择结束日期" :picker-options="pickerOptions2" @change="endDateChange" style="width:100%"></el-date-picker>
                                    </el-col>
                                </el-row>     
                            </el-form-item>
                        </el-col>
                        <el-col :span="3" :offset="7">
                            <div class="top-line">
                                <el-button type="primary" class="btn-common" @click="onSubmit('ruleForm')">查询</el-button>
                            </div>
                        </el-col>
                    </el-row> 
                </el-form>
            </div> 
            <div slot="table" >
                <div class="table-title">
                    <export-excel :jsonStr="jsonStr"></export-excel>
                </div> 
                <el-table :empty-text="emptyText" :data="tableData" border v-loading="loading" style="width: 100%"  @sort-change="sortTable">
    
                    <el-table-column label="用户姓名" align="center">
                        <template scope="scope">
                            <span>{{scope.row.s_name | isNull}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="e学云号" align="center" >
                        <template scope="scope">
                            <span>{{scope.row.s_exue_code | isNull}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="使用时长" align="center" sortable="custom" prop="useTime">
                        <template scope="scope">
                            <span>{{scope.row.useTime | formatDuring}}</span>
                        </template>
                    </el-table-column>
    
                    <el-table-column label="登录次数" align="center" sortable="custom" prop="login">
                        <template scope="scope">
                            <span>{{scope.row.login | isNull}}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="注册时间" align="center">
                        <template scope="scope">
                            <span>{{scope.row.s_register_time | isNull}}</span>
                        </template>
                    </el-table-column>
<!-- 
                    <el-table-column label="报表日期" align="center">
                        <template scope="scope">
                            <span>{{scope.row.business | isNull}}</span>
                        </template>
                    </el-table-column> -->

                </el-table>
                <div class="pagination-container">
                    <el-pagination
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pageQuery.total"
                        :current-page.sync="pageQuery.currentpage"
                        :page-sizes="config.pageSizes"
                        :page-size="config.pageSize"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange">
                    </el-pagination>
                </div>
            </div>
        </echartsLayout>
    </div>
</template>

<script>
    import moment from 'moment';
    import exportExcel from 'components/ExportExcel/index';
    import echartsLayout from 'components/TableLayout/echartsLayout'; 
    import { mapState, mapActions } from 'vuex';
    // import { getReportFormData } from 'api/reportForms';
    import { queryTable } from 'api/common.js'
    export default{
        components: {echartsLayout, exportExcel},
        data() {
            return {
                loading: false,
                formState: false,
                dataListState: true,
                eCharts1State: true,
                eCharts2State: true,
                eCharts3State: true,
                chartsListState: true,
                tableState: false,
                query: {
                    eID: '',
                    startDate: new Date(),
                    endDate: new Date()
                },
                tableData: [],
                pageQuery: {
                    currentpage: 1,
                    total: 0
                },
                pickerOptions1: null,
                pickerOptions2: null,
                sortType: '', // 排序类型
                isDesc: true, // 是否是降序
                queryId: 30,
                showList: true,
                realTime: false,
                type: 'event',
                rules: {
                    eID: [
                        { pattern: /^[0-9]{9}$/, trigger: 'blur', message: '请输入完整的e学云ID'}
                    ]
                },
                jsonStr: null
            }
        },
        computed: {
            ...mapState({
                config: state => state.config.table
            }),
            emptyText() {
                return this.pageQuery.currentpage == 1 ? '暂无数据' : '为保障流畅性，暂展示一万条数据，您可查询所需信息';
            }

        },
        mounted() {
            this.queryData();
        },
        methods: {
            ...mapActions(['ChangePageSize']),
            handleSizeChange(val) {
                this.pageQuery.currentpage = 1;
                this.ChangePageSize(val);
                this.queryData();
            },
            handleCurrentChange(val) {
                this.pageQuery.currentpage = val;
                this.queryData();
            },

            startDateChange(date) {
                this.pickerOptions2 = {
                    disabledDate(time) {
                        return (time.getTime() < new Date(date).getTime() - 864e5);
                    }
                }
            },
            endDateChange(date) {
                this.pickerOptions1 = {
                    disabledDate(time) {
                        return (time.getTime() > new Date(date).getTime());
                    }
                } 
            },

            // 查询
            onSubmit() {
                this.pageQuery.currentpage = 1;
                this.queryData();
            },

            // 查询数据
            queryData() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        let startDate = this.query.startDate==''?'':moment(this.query.startDate).format('YYYY-MM-DD'); 
                        let endDate = this.query.endDate === ''?moment().format('YYYY-MM-DD'):moment(this.query.endDate).format('YYYY-MM-DD');
                        this.sortType = this.sortType=== ''?'login':this.sortType;
                        let query = {
                            'order_field': this.sortType,
                            'order_desc': this.isDesc, 
                            'exueCode': this.query.eID, 
                            'from_date': startDate, 
                            'to_date': endDate
                        };
                        queryTable(query, this.queryId, this.pageQuery.currentpage, this.config.pageSize, this.showList, this.type, this.realTime).then(response => {
                            this.loading = false;
                            if (response.data.content) {
                                this.tableData = response.data.content.rows;
                                this.pageQuery.total = response.data.content.total || response.data.content.total_rows;
                            }
                        }).catch(() => {
                            this.loading = false;
                        });

                        // 导出
                        let fileName = '用户登陆情况';
                        let fieldNames = [['用户姓名', 's_name'], ['e学云号', 's_exue_code'], ['使用时长', 'useTime'], ['登录次数', 'login'], ['注册时间', 's_register_time']];
                        this.jsonStr = {
                            query_id: this.queryId,
                            real_time: this.realTime,
                            query,
                            type: this.type,
                            show_list: this.showList,
                            file_name: fileName,
                            field_names: fieldNames
                        }
                    } 
                });
                
            },

            // 列表排序
            sortTable(params) {
                this.sortType = params.prop;
                params.order === 'ascending'?this.isDesc = false:this.isDesc = true;
                this.queryData();
            }

            
        }
    }
</script>
<style lang="scss" scoped>
    .main-content {
        position: relative;
        $bgcolor: #fff;
    }
</style>                     